package com.wits.av;

import android.util.Log;

/**
 * Created by zhengboyuan on 2018-03-13.
 */

public class G711Encoder {

    private static final String TAG = "jni";

    int handle = JniG711Encoder.INVALID_HANDLE;

    public G711Encoder() {

    }

    public boolean open(int codec, int freq, int channels) {
        Log.i(TAG, "G711Encoder.open ");

        if (isOpen()) {
            close();
        }

        handle = JniG711Encoder.g711_open(codec, freq, channels);

        Log.i(TAG, "G711Encoder.open return " + handle);
        return isOpen();
    }

    public void close() {
        Log.i(TAG, "G711Encoder.close " + handle);

        if (isOpen()) {
            JniG711Encoder.g711_close(handle);
            handle = JniG711Encoder.INVALID_HANDLE;
        }

        Log.i(TAG, "G711Encoder.close end ");
    }

    public boolean isOpen() {
        return (handle != JniG711Encoder.INVALID_HANDLE);
    }

    public boolean encode(byte[] audioData, int length, long pts, MPacket pkt) {
        if (handle == JniG711Encoder.INVALID_HANDLE) {
            Log.w(TAG, "G711Encoder.encode. handle is invalid.");
            return false;
        }

        int ret = JniG711Encoder.g711_encode(handle, audioData, length, pts, pkt);
        return (ret == 0);
    }
}
